-------------------------------------------------
--  utils_table.lua
--  Created by DX on 17-3-31.
--  Fixed by .
-------------------------------------------------
--排序
function table.sort_(t , field , reverse)
	--比较器 正序
	function inner_func_compare1(a, b)
		return number_val(a[field]) < number_val(b[field])
	end

	--比较器 逆序
	function inner_func_compare2(a, b)
		return number_val(a[field]) > number_val(b[field])
	end

	if reverse then
		table.sort(t, inner_func_compare2)
	else
		table.sort(t, inner_func_compare1)
	end

	return t
end

-- 双参数 双条件排序 field 大条件  field2 小条件
function table.dualSort(t ,field ,field2,reverse)
	function inner_func_compare(a, b)
		if number_val(a[field]) == number_val(b[field]) then
			return number_val(a[field2]) < number_val(b[field2])
		else
			return number_val(a[field]) < number_val(b[field])
		end
	end
	
	table.sort(t, inner_func_compare)
	if reverse then
		t = table.reverse(t)
	end
	return t 
end

-- 双参数 百分比排序 field 分子  field2 分母
function table.percentageSort(t ,field ,field2,reverse)
	function inner_func_compare(a, b)
		print(a[field])
		print(a[field2])
		print(b[field])
		print(b[field2])
		return number_val(a[field]/a[field2]) < number_val(b[field]/b[field2])
	end
	
	table.sort(t, inner_func_compare)
	if reverse then
		t = table.reverse(t)
	end
	return t 
end


function table.group(t , count)
	local t1 = {}
	
	for k,v in ipairs(t) do
		if t1[math.ceil(k/count)] == nil then
			t1[math.ceil(k/count)] = {}
		end
		table.insert(t1[math.ceil(k/count)], v)
	end
	
	return t1
end

function table.groupOfDay(t , field)
	local t1 = {}
	
	for k,v in ipairs(t) do
        local day = math.floor(number_val(v[field]) / 86400)
        if k ~= 1 and day == math.floor(number_val(t1[#t1][1][field]) / 86400)  then
            table.insert(t1[#t1],v)
        else
	        if t1[#t1 + 1] == nil then
                t1[#t1 + 1] = {}
		    end
		    table.insert(t1[#t1], v)
		end
	end
	
	return t1
end


function table.findOfValue(t,field,value)
	for k,v in ipairs(t) do
		if v[field] == value then
			return v
		end
	end
	return nil
end

function table.find(t,value)
	for k,v in ipairs(t) do
		if v == value then
			return k
		end
	end
	return -1
end

function table.findByFunc(t, compareFunc)
	for k,v in ipairs(t) do
		if compareFunc(v) then
			return k
		end
	end
	return -1
end

function table.contain_value(t,value)
	for i,v in pairs(t) do
		if v==value then
			return true
		end
	end
	return false
end

--数据字典
function table.dictionary(t,field)
	local dict={}
	for i,v in ipairs(t) do
		if v[field] then
			dict[tostring(v[field])]=v
		end
	end
	return dict
end

--逆序
function table.reverse(t)
	local tCount = #t
	for i=1,tCount do
		local one = table.remove(t,1)
		table.insert(t,tCount-(i-1),one)
	end
	return t
end

--复制
function table.copy(t)
	local t2 = {}
	for k,v in pairs(t) do
		t2[k] = v
	end
	return t2
end

--移除table中项与value相等的一项
function table.remove_value(t,value)
	for i,v in pairs(t) do
		if v==value then
			table.remove(t,i)
			return i
		end
	end
end

function table.remove_by_func(t,func)
	local index = table.findByFunc(t, func)
	if index > 0 then
		table.remove(t, index)
	end

	return index
end

function table.merge(t1,t2)
	table.foreach(t2, function(i, v)
		table.insert(t1,v)
	end)
	return t1
end

function table.shift(t)
	return table.remove(t,1)
end

function table.push(t , value)
	local t1 = {}
	table.insert(t1 , value)
	return table.merge(t1,t)
end

function table.copyValues(t1, t2)
	if t2 then
		for k, v in pairs(t2) do
			t1[k] = v;
		end
	end
	return t1;
end

function table.toArray(t, elementType)
	if not t then
		return nil;
	end

	local array = System.Array.CreateInstance(elementType, #t);
	for k, v in ipairs(t) do
		array:SetValue(v, k - 1);
	end
	return array;
end

function table.isArray(t)
	if type(t) ~= "table" then
		return false;
	end

	local maxn = 0;
	for k, _ in pairs(t) do
		if type(k) ~= "number" then
			return false;
		end

		if math.floor(k) ~= k or k < 1 then
			return false;
		end

		if maxn < k then
			maxn = k;
		end
	end

	for i = 1, maxn do
		if t[i] == nil then
			return false;
		end
	end

	return true;
end

function utils.tableEqual(t1,t2)
	if #t1 == #t2 then
		for i = 1 , #t1 do
			if t1[i] ~= t2[i] then
				return false
			end
		end
	else
		return false
	end
	return true
end

function utils.equal(t1,t2)
	for k,v in pairs(t1) do
		if t2[k] ~= v then
			return false
		end
	end
	return true
end

function table.insert_mvvmlist(t , value , index)
	local t1 = t
	if index then
		table.insert(t1,value,index)
	else
		table.insert(t1,value)
	end
	t = t1
	return t
end